package com.health.fragment.focusgroup.child;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.health.viewscontrols.NovaApplication;
import com.health.viewscontrols.PersonalFileActivity.onFragmentSave;
import com.health.R;
import com.health.db.FDbManager;
import com.health.viewscontrols.focusgroup.ChildActivity;
import com.health.fragment.NovaFragment;
import com.health.tablebean.ARCHIVE_BASEINFO;
import com.health.tablebean.BD_DD_INFO;
import com.health.tablebean.CHILD_BASEINFO;
import com.health.tools.GlobalConts;
import com.health.tools.IDCardVerifyTool;
import com.health.widget.EditTextWithDate;

import android.graphics.Color;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

/**
 * 儿童管理卡
 * 
 * @author 鹿云龙
 */
public class ErTongGuanLiKaFragment extends NovaFragment implements onFragmentSave {
	// CHILD_BASEINFO
	// private CHILD_BASEINFO baseInfo;
	private View baseView = null;
	private ARCHIVE_BASEINFO userInfo;// 基本信息
	// private MyGridView mgvMqsfzh;// 母亲身份证号
	private FDbManager fdbmanger = null;
	private String IDCARD;
	private RadioButton id1;// 15位
	private RadioButton id2;// 18位
	/** 母亲身份证号位数 */
	private List<BD_DD_INFO> lstMqsfzh;
	/** 卡号 */
	private EditText etCARDNUM;
	/** 母亲身份证号 */
	private EditText etMOTHER_IDCARD;
	/** 儿童姓名 */
	private EditText et_rtxm;
	/** 性别 */
	private EditText etSEX;
	/** 建档时间 */
	private EditTextWithDate etCREATE_DATE;
	/** 出生日期 */
	private EditText etBIRTHDAY;
	/** 父母或监护人情况 */
	private EditText etGUARDSTATUS;
	/** 父亲姓名 */
	private EditText etFARTHERNAME;
	/** 父亲年龄 */
	private EditText etFATHERAGE;
	/** 父亲工作单位 */
	private EditText etFATHERUNIT;
	/** 父亲联系电话 */
	private EditText etFATHERPHONE;
	/** 母亲姓名 */
	private EditText etMOTHERNAME;
	/** 母亲年龄 */
	private EditText et_mqnl;
	/** 母亲工作单位 */
	private EditText etMOTHERUNIT;
	/** 母亲联系电话 */
	private EditText etMOTHERPHONE;
	/** 监护人姓名 */
	private EditText etGUARDERNAME;
	/** 监护人年龄 */
	private EditText et_jhrnl;
	/** 监护人工作单位 */
	private EditText etGUARDERUNIT;
	/** 监护人联系电话 */
	private EditText etGUARDERPHONE;
	/** 住址 */
	private EditText etADDR;
	/** 邮政编码 */
	private EditText etPOSTAL_CODE;

	/** 建档单位 */
	private EditText etCREATE_UNIT;
	/** 建档单位电话 */
	private EditText etCREATEUNIT_PHONE;
	/** 托幼机构 */
	private EditText etCHILDCARE;
	/** 托幼机构电话 */
	private EditText etCHILDCARE_PHONE;
	/** 母亲身份证号15位 */
	private RadioButton s;
	/** 母亲身份证号18位 */
	private RadioButton ss;
	private List<BD_DD_INFO> bdxingbie;
	private RadioGroup rd_idcard; 

	@Override
	public View findViews(LayoutInflater inflater) {
		baseView = inflater.inflate(R.layout.ertong_rtglk_fragment, null);
		fdbmanger = NovaApplication.fdbmanger;
		userInfo = NovaApplication.getUserInfo();
		IDCARD = NovaApplication.getUserInfo().getIDCARD();

		// mgvMqsfzh = (MyGridView) baseView.findViewById(R.id.mgv_mqsfzh);
		lstMqsfzh = new ArrayList<BD_DD_INFO>();
		// bindZhengZhuangData();
		// lstMqsfzh
		BD_DD_INFO bdi1 = new BD_DD_INFO();
		BD_DD_INFO bdi2 = new BD_DD_INFO();

		bdi1.setID("18位");
		bdi1.setDATAINFO("15位");
		bdi2.setID("15位");
		bdi2.setDATAINFO("18位");
		lstMqsfzh.add(bdi1);
		lstMqsfzh.add(bdi2);
		bdxingbie = FDbManager.getInstance(getActivity()).findAllByWhereInInfoDB(BD_DD_INFO.class,
				"PARENTID = 'DM01-08'");

		setupView(baseView);
		addListener();
		// bind();
		initData();
		GlobalConts.setMaxLength(20, etCARDNUM);/** 卡号 */
		GlobalConts.setMaxLength(1, etSEX);/** 性别 */

		// TODO 未找到此列 etGUARDSTATUS;/** 父母或监护人情况 */
		GlobalConts.setMaxLength(25, etFARTHERNAME, // ** 父亲姓名 */
				etFATHERUNIT, // ** 父亲工作单位 */
				etMOTHERNAME, // ** 母亲姓名 */
				etMOTHERUNIT, // ** 母亲工作单位 */
				etGUARDERNAME, // ** 监护人姓名 */
				etGUARDERUNIT// ** 监护人工作单位 */
		);
		GlobalConts.setMaxLength(35, etADDR, // ** 住址 */
				etCREATE_UNIT, // 建档单位 */
				etCHILDCARE);// 托幼机构 */

		return baseView;
	}

	private int from;

	public void initData() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd ");
		String date = formatter.format(new Date());
		CHILD_BASEINFO baseInfo;

		et_rtxm.setText(userInfo.getCUSTOMERNAME());
		// TODO
		// bdxingbie
		String sex = "";
		for (BD_DD_INFO bdi : bdxingbie) {
			if (userInfo.getSEX().equals(bdi.getID())) {
				sex = bdi.getDATAINFO();
			}
		}
		etSEX.setText(sex);
		try {
			etBIRTHDAY.setText(formatter.format(userInfo.getBIRTHDAY()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<CHILD_BASEINFO> acs = fdbmanger.findAllByWhere(CHILD_BASEINFO.class,
				"IDCARD = '" + userInfo.getIDCARD() + "'");
		if (acs.size() > 0) {
			from = 1;
			baseInfo = acs.get(acs.size() - 1);

			etADDR.setText(baseInfo.getADDR());
			// baseInfo.setARCHIVEID(et_da

			etBIRTHDAY.setText(formatter.format(userInfo.getBIRTHDAY()));
			etCARDNUM.setText(baseInfo.getCARDNUM());
			etCHILDCARE.setText(baseInfo.getCHILDCARE());
			etCHILDCARE_PHONE.setText(baseInfo.getCHILDCARE_PHONE());
			try {
				etCREATE_DATE.setText(formatter.format(baseInfo.getCREATED_DATE()));
			} catch (Exception e) {
				e.printStackTrace();
			}
			etCREATE_UNIT.setText(baseInfo.getCREATEUNIT());
			etCREATEUNIT_PHONE.setText(baseInfo.getCREATEUNIT_PHONE());
			etFARTHERNAME.setText(baseInfo.getFARTHERNAME());
			etFATHERAGE.setText(baseInfo.getFATHERAGE());
			etFATHERPHONE.setText(baseInfo.getFATHERPHONE());
			etFATHERUNIT.setText(baseInfo.getFATHERUNIT());
			etGUARDERNAME.setText(baseInfo.getGUARDERNAME());
			etGUARDERPHONE.setText(baseInfo.getGUARDERPHONE());
			etGUARDERUNIT.setText(baseInfo.getGUARDERUNIT());
			etGUARDSTATUS.setText(baseInfo.getGUARDSTATUS());
			// baseInfo.getIS_DEL(et_.getT
			// baseInfo.getLAST_UPDATE_BY(
			// baseInfo.getLAST_UPDATE_DAT(
			// baseInfo.getMOTHER_ID(et_.g
			et_jhrnl.setText(baseInfo.getGUARDERAGE());
			etMOTHER_IDCARD.setText(baseInfo.getMOTHER_IDCARD());
			String strid = etMOTHER_IDCARD.getText().toString();
			
			if (!TextUtils.isEmpty(strid)) {
				if (strid.length() == 15) {
					id1.setChecked(true);

				} else if (strid.length() == 18) {
					id2.setChecked(true);
				}
			} else {
				id2.setChecked(true);
			}

			et_mqnl.setText(baseInfo.getMOTHERAGE());
			etMOTHERNAME.setText(baseInfo.getMOTHERNAME());
			etMOTHERPHONE.setText(baseInfo.getMOTHERPHONE());
			etMOTHERUNIT.setText(baseInfo.getMOTHERUNIT());
			// baseInfo.getOCCURRENCE_TIME
			etPOSTAL_CODE.setText(baseInfo.getPOSTAL_CODE());
			// etSEX.setText(baseInfo.getSEX());
		}
	}

	private void saveData() {


		// 父亲
		String str_etFATHERPHONE = etFATHERPHONE.getText().toString();
		if (!TextUtils.isEmpty(str_etFATHERPHONE)) {
			if (!GlobalConts.isPhoneNumberValid(str_etFATHERPHONE)) {
				etFATHERPHONE.setBackgroundColor(Color.YELLOW);
				((ChildActivity) getActivity()).showTextDialog("请输入正确的父亲联系电话");
				return;
			} else {
				etFATHERPHONE.setBackgroundColor(Color.WHITE);
			}
		} else {
			etFATHERPHONE.setBackgroundColor(Color.WHITE);
		}
		// 母亲 电话
		String str_etMOTHERPHONE = etMOTHERPHONE.getText().toString();
		if (!TextUtils.isEmpty(str_etMOTHERPHONE)) {
			if (!GlobalConts.isPhoneNumberValid(str_etMOTHERPHONE)) {
				etMOTHERPHONE.setBackgroundColor(Color.YELLOW);
				((ChildActivity) getActivity()).showTextDialog("请输入正确的母亲联系电话");
				return;
			} else {
				etMOTHERPHONE.setBackgroundColor(Color.WHITE);
			}
		} else {
			etMOTHERPHONE.setBackgroundColor(Color.WHITE);
		}
		
		//邮政编号
		String str_etPOSTAL_CODE = etPOSTAL_CODE.getText().toString();
		if (!TextUtils.isEmpty(str_etPOSTAL_CODE)) {
			if (!GlobalConts.checkPost(str_etPOSTAL_CODE)) {
				etPOSTAL_CODE.setBackgroundColor(Color.YELLOW);
				((ChildActivity) getActivity()).showTextDialog("请输入正确的邮政编号");
				return;
			} else {
				etPOSTAL_CODE.setBackgroundColor(Color.WHITE);
			}
		} else {
			etPOSTAL_CODE.setBackgroundColor(Color.WHITE);
		}
		
		//建档单位电话
		String str_etCREATEUNIT_PHONE = etCREATEUNIT_PHONE.getText().toString();
		if (!TextUtils.isEmpty(str_etCREATEUNIT_PHONE)) {
			if (!GlobalConts.isPhoneNumberValid(str_etCREATEUNIT_PHONE)) {
				etCREATEUNIT_PHONE.setBackgroundColor(Color.YELLOW);
				((ChildActivity) getActivity()).showTextDialog("请输入正确的建档单位电话");
				return;
			} else {
				etCREATEUNIT_PHONE.setBackgroundColor(Color.WHITE);
			}
		} else {
			etCREATEUNIT_PHONE.setBackgroundColor(Color.WHITE);
		}
		
		//托幼机构电话
		String str_etCHILDCARE_PHONE = etCHILDCARE_PHONE.getText().toString();
		if (!TextUtils.isEmpty(str_etCHILDCARE_PHONE)) {
			if (!GlobalConts.isPhoneNumberValid(str_etCHILDCARE_PHONE)) {
				etCHILDCARE_PHONE.setBackgroundColor(Color.YELLOW);
				((ChildActivity) getActivity()).showTextDialog("请输入正确的托幼机构电话");
				return;
			} else {
				etCHILDCARE_PHONE.setBackgroundColor(Color.WHITE);
			}
		} else {
			etCHILDCARE_PHONE.setBackgroundColor(Color.WHITE);
		}
		
		
		

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		CHILD_BASEINFO baseInfo = new CHILD_BASEINFO();
		String motherId = etMOTHER_IDCARD.getText().toString();
		if (!TextUtils.isEmpty(motherId)) {
//			if (motherId.length() == 15) {
//				motherId = IDCardVerifyTool.from15to18(20, motherId);
//			}
			if (!IDCardVerifyTool.Validate(motherId).equals("")) {
				((ChildActivity) getActivity()).showTextDialog("身份证错误，保存失败");
				return;
			}
		}
		baseInfo.setIDCARD(userInfo.getIDCARD());
		baseInfo.setADDR(etADDR.getText().toString());
		baseInfo.setARCHIVEID("");// 档案编号
		try {
			baseInfo.setBIRTHDAY(sdf.parse(etBIRTHDAY.getText().toString()));
		} catch (ParseException e1) {
			e1.printStackTrace();
		}
		baseInfo.setCARDNUM(etCARDNUM.getText().toString());
		baseInfo.setCHILDCARE(etCHILDCARE.getText().toString());
		if (!TextUtils.isEmpty(etCHILDCARE_PHONE.getText().toString())) {
			if (GlobalConts.isPhoneNumberValid(etCHILDCARE_PHONE.getText().toString().trim())) {
				baseInfo.setCHILDCARE_PHONE(etCHILDCARE_PHONE.getText().toString());
			} else {
				GlobalConts.showToast(getActivity(), "托幼机构电话不正确，请重输");
				return;
			}
		} else {
			baseInfo.setCHILDCARE_PHONE(etCHILDCARE_PHONE.getText().toString());
		}

		baseInfo.setCREATED_BY(userInfo.getCREATED_BY());// 创建人
		try {
			baseInfo.setCREATED_DATE(sdf.parse(etCREATE_DATE.getText().toString()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		baseInfo.setCREATEUNIT(etCREATE_UNIT.getText().toString());
		if (!TextUtils.isEmpty(etCREATEUNIT_PHONE.getText().toString())) {
			if (GlobalConts.isPhoneNumberValid(etCREATEUNIT_PHONE.getText().toString().trim())) {
				baseInfo.setCREATEUNIT_PHONE(etCREATEUNIT_PHONE.getText().toString());
			} else {
				GlobalConts.showToast(getActivity(), "建档单位电话不正确，请重新输入");
				return;
			}
		} else {
			baseInfo.setCREATEUNIT_PHONE(etCREATEUNIT_PHONE.getText().toString());
		}

		baseInfo.setCUSTOMERID("");// 客户编号
		baseInfo.setFARTHERNAME(etFARTHERNAME.getText().toString());
		baseInfo.setFATHER_ID("");// 父亲档案编号
		baseInfo.setFATHERAGE(etFATHERAGE.getText().toString());
		baseInfo.setFATHERPHONE(etFATHERPHONE.getText().toString());
		baseInfo.setFATHERUNIT(etFATHERUNIT.getText().toString());
		String jhrnl = et_jhrnl.getText().toString();
		baseInfo.setGUARDERAGE(jhrnl);
		baseInfo.setGUARDERNAME(etGUARDERNAME.getText().toString());
		baseInfo.setGUARDERPHONE(etGUARDERPHONE.getText().toString());
		baseInfo.setGUARDERUNIT(etGUARDERUNIT.getText().toString());
		baseInfo.setGUARDSTATUS(etGUARDSTATUS.getText().toString());
		baseInfo.setIS_DEL("");// 删除标志Y删除N未删除
		// baseInfo.setLAST_UPDATE_BY(0);//最后更新人
		baseInfo.setLAST_UPDATE_DATE(new Date());// 更新时间(使用系统时间)

		// baseInfo.setMOTHER_ID(0);
		baseInfo.setMOTHER_IDCARD(etMOTHER_IDCARD.getText().toString());
		String mqnl = et_mqnl.getText().toString();
		baseInfo.setMOTHERAGE(mqnl);
		baseInfo.setMOTHERNAME(etMOTHERNAME.getText().toString());
		baseInfo.setMOTHERPHONE(etMOTHERPHONE.getText().toString());
		baseInfo.setMOTHERUNIT(etMOTHERUNIT.getText().toString());
		baseInfo.setOCCURRENCE_TIME(new Date());// 发生时间
		baseInfo.setPOSTAL_CODE(etPOSTAL_CODE.getText().toString());
		// baseInfo.setSEX(etSEX.getText().toString());

		if (from == 0) {
			baseInfo.setCREATED_BY(userInfo.getCREATER());
			baseInfo.setCREATED_DATE(new Date());
			baseInfo.setCREATEORG(userInfo.getCREATEORG());
			baseInfo.setCREATER(userInfo.getCREATER());
			FDbManager.getInstance(getActivity()).save(baseInfo);
			from = 1;
		} else {
			baseInfo.setLAST_UPDATE_BY(userInfo.getCREATER());
			baseInfo.setLAST_UPDATE_DATE(new Date());
			baseInfo.setMODIFIED(userInfo.getCREATER());
			baseInfo.setCREATEORG(userInfo.getCREATEORG());
			FDbManager.getInstance(getActivity()).updata(baseInfo, "IDCARD='" + userInfo.getIDCARD() + "'");
		}
		
		XinShengErFangShiFragment.setFatherMatherData();
		((ChildActivity) getActivity()).showTextDialog("保存成功");

	}

	public void bindIDLength() {
		// RadioButtonAdapter rba = new RadioButtonAdapter(lstMqsfzh,
		// getActivity());
		// mgvMqsfzh.setAdapter(rba);

	}

	/**
	 * 获取下拉框中需要的数据的信息
	 * 
	 * @param datas
	 * @return
	 * @author 鹿云龙
	 * @date 2015年12月16日 下午3:47:00
	 */
	private ArrayList<String> getDataInfo(List<BD_DD_INFO> datas) {
		ArrayList<String> infos = new ArrayList<String>();
		for (BD_DD_INFO bd : datas) {
			infos.add(bd.getDATAINFO());
		}
		return infos;
	}

	@Override
	public void setupView(View view) {
		etCARDNUM = (EditText) baseView.findViewById(R.id.et_kahao);
		etMOTHER_IDCARD = (EditText) baseView.findViewById(R.id.et_mqsfzh);
		// etMOTHER_IDCARD.setInputType(InputType.TYPE_CLASS_NUMBER);

		et_rtxm = (EditText) baseView.findViewById(R.id.et_rtxm);
		etSEX = (EditText) baseView.findViewById(R.id.et_xb);
		etBIRTHDAY = (EditText) baseView.findViewById(R.id.et_csrq);
		etCREATE_DATE = (EditTextWithDate) baseView.findViewById(R.id.etw_jdsj);
		etGUARDSTATUS = (EditText) baseView.findViewById(R.id.et_fmhjhrqk);
		etFARTHERNAME = (EditText) baseView.findViewById(R.id.et_fqxm);
		etFATHERAGE = (EditText) baseView.findViewById(R.id.et_fqnl);
		etFATHERUNIT = (EditText) baseView.findViewById(R.id.et_fqgzdw);
		etFATHERPHONE = (EditText) baseView.findViewById(R.id.et_fqlxdh);
		etMOTHERNAME = (EditText) baseView.findViewById(R.id.et_mqxm);
		et_mqnl = (EditText) baseView.findViewById(R.id.et_mqnl);
		etMOTHERUNIT = (EditText) baseView.findViewById(R.id.et_mqgzdw);
		etMOTHERPHONE = (EditText) baseView.findViewById(R.id.et_mqlxdh);
		etGUARDERNAME = (EditText) baseView.findViewById(R.id.et_jhrxm);
		et_jhrnl = (EditText) baseView.findViewById(R.id.et_jhrnl);
		etGUARDERUNIT = (EditText) baseView.findViewById(R.id.et_jhrgzdw);
		etGUARDERPHONE = (EditText) baseView.findViewById(R.id.et_jhrlxdh);
		etADDR = (EditText) baseView.findViewById(R.id.et_zhuzhi);
		etPOSTAL_CODE = (EditText) baseView.findViewById(R.id.et_yzbm);
		etCREATE_UNIT = (EditText) baseView.findViewById(R.id.et_jddw);
		etCREATEUNIT_PHONE = (EditText) baseView.findViewById(R.id.et_jddwdh);
		etCHILDCARE = (EditText) baseView.findViewById(R.id.et_tyjg);
		etCHILDCARE_PHONE = (EditText) baseView.findViewById(R.id.et_tyjgdh);
		id1 = (RadioButton) baseView.findViewById(R.id.id1);
		id2 = (RadioButton) baseView.findViewById(R.id.id2);
		rd_idcard=(RadioGroup)baseView.findViewById(R.id.rd_idcard);
	}

	@Override
	public void addListener() {
		rd_idcard.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if (checkedId == R.id.id1) {
					int maxLengthofEditText = 15;
					etMOTHER_IDCARD.setFilters(new InputFilter[] { new InputFilter.LengthFilter(maxLengthofEditText) });
				} else if (checkedId == R.id.id2) {
					int maxLengthofEditText = 18;
					etMOTHER_IDCARD.setFilters(new InputFilter[] { new InputFilter.LengthFilter(maxLengthofEditText) });
				}  
			}
		});


	}

	@Override
	public void setData() {
	}

	public int getListIndex(String id, List<BD_DD_INFO> bds) {
		int index = 0;
		try {
			for (int i = 0; i < bds.size(); i++) {
				if (id.equals(bds.get(i).getID())) {
					index = i;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return index;
	}

	/**
	 * 字符串转换"1,2,3,4,5" --> "'1','2','3','4','5'"
	 * 
	 * @param symptom
	 * @return
	 * @author 鹿云龙
	 * @date 2015年12月19日 下午2:49:23
	 */
	public String checkedIdsSql(String symptom) {
		String[] idarr = symptom.split(",");
		String sql = "";
		for (String id : idarr) {
			sql += "'" + id + "',";
		}
		sql = sql.substring(0, sql.length() - 1);
		System.out.println(sql);
		return sql;
	}

	@Override
	public void save() {
		saveData();
	}

	@Override
	public void onStart() {
		super.onStart();
	}
	
	
}